// GTY


#include "AbilitySystem/Abilities/AuraFireBolt.h"

#include "AbilitySystem/AuraAbilitySystemLibrary.h"
#include "Actor/AuraProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Kismet/KismetSystemLibrary.h"


FString UAuraFireBolt::GetDescription(const int32 Level)
{
	return GetDescriptionAtLevel(Level, L"火球术");
}

FString UAuraFireBolt::GetNextLevelDescription(const int32 Level)
{
	return GetDescriptionAtLevel(Level, L"下一等级");
}

FString UAuraFireBolt::GetDescriptionAtLevel(int32 Level, const FString& Title)
{

	//根据等级获取技能伤害
	const int32 ScaledDamage = Damage.GetValueAtLevel(Level);
	const float ManaCost = GetManaCost(Level);
	const float Cooldown = GetCooldown(Level);
	
	return FString::Printf(TEXT(
		// 标题
		"<Title>%s</>\n"

		// 细节
		"<Small>等级：</> <Level>%i</>\n"
		"<Small>技能冷却：</> <Cooldown>%.1f</>\n"
		"<Small>蓝量消耗：</> <ManaCost>%.1f</>\n\n"//%.1f会四舍五入到小数点后一位

		// 技能描述
		"<Default>发射 %i 颗火球，在发生撞击时产生爆炸，并造成</> <Damage>%i</> <Default>点火焰伤害，并有一定几率燃烧。</>"),

		// 动态修改值
		*Title,
		Level,
		Cooldown,
		ManaCost,
		FMath::Min(Level, NumProjectiles),
		ScaledDamage);
}

void UAuraFireBolt::SpawnProjectiles(
	const FVector& ProjectileTargetLocation, const FGameplayTag& SocketTag, const FName SocketName,
	const bool bOverridePitch, const float PitchOverride, AActor* HomingTarget
)
{
	ICombatInterface* CombatInterface = Cast<ICombatInterface>(GetAvatarActorFromActorInfo());//获取角色
	if (CombatInterface)
	{
		const FVector SocketLocation = ICombatInterface::Execute_GetCombatSocketLocation(GetAvatarActorFromActorInfo(),SocketTag);

		FRotator SocketRotation = (ProjectileTargetLocation - SocketLocation).Rotation();//获取目标位置和角色位置的向量，并计算旋转
		
		if (bOverridePitch)		{ SocketRotation.Pitch = PitchOverride;}//是否覆盖角度
		
		const FVector Forward = SocketRotation.Vector();//获取面向方向
		const int32 EffectiveNumProjectiles = FMath::Min(NumProjectiles, GetAbilityLevel());

		
		TArray<FRotator> Rotators = UAuraAbilitySystemLibrary::EvenlySpacedRotators(Forward, FVector::UpVector, ProjectileSpread, EffectiveNumProjectiles);
		
		for (FRotator& Rotator : Rotators)
		{
			//UKismetSystemLibrary::DrawDebugArrow(GetAvatarActorFromActorInfo(), SocketLocation, SocketLocation + Rotator.Vector() * 50.f, 5, FLinearColor::Red, 120, 3);
			FTransform SpawnTransform;
			SpawnTransform.SetLocation(SocketLocation);//设置位置
			SpawnTransform.SetRotation(Rotator.Quaternion());//设置旋转
	
			AAuraProjectile* Projectile = GetWorld()->SpawnActorDeferred<AAuraProjectile>//延迟生成
			(
				ProjectileClass,//指定生成的演员类。
				SpawnTransform,//指定生成位置和旋转。
				GetOwningActorFromActorInfo(),//指定生成的演员的拥有者。
				Cast<APawn>(GetOwningActorFromActorInfo()),//指定生成的演员的归属者。
				ESpawnActorCollisionHandlingMethod::AlwaysSpawn//指定生成的演员的碰撞处理方式。始终生成。
			);
			Projectile->DamageEffectParams = MakeDamageEffectParamsFromClassDefaults();

			//跟踪
			if (HomingTarget && HomingTarget->Implements<UCombatInterface>())
			{
				//设置攻击的位置为攻击对象的根位置
				Projectile->ProjectileMovement->HomingTargetComponent = HomingTarget->GetRootComponent();
			}
			else
			{
				//如果没有获取到攻击目标，则创建一个可销毁的并应用
				Projectile->HomingTargetSceneComponent = NewObject<USceneComponent>(USceneComponent::StaticClass());
				Projectile->HomingTargetSceneComponent->SetWorldLocation(ProjectileTargetLocation); //设置组件位置
				Projectile->ProjectileMovement->HomingTargetComponent = Projectile->HomingTargetSceneComponent;
			
			}
			//设置飞弹朝向目标时的加速度
			Projectile->ProjectileMovement->HomingAccelerationMagnitude = FMath::FRandRange(HomingAccelerationMin, HomingAccelerationMax);
			Projectile->ProjectileMovement->bIsHomingProjectile = bLaunchHomingProjectiles; //设置为true，飞弹将加速飞向攻击目标
		

			
			Projectile->FinishSpawning(SpawnTransform);//完成生成
		}
	}


}

FVector UAuraFireBolt::GetForward(
	const FVector& ProjectileTargetLocation, const FGameplayTag& SocketTag, const bool bOverridePitch,
	const float PitchOverride
)
{
	FVector Forward = FVector::Zero();
	ICombatInterface* CombatInterface = Cast<ICombatInterface>(GetAvatarActorFromActorInfo());//获取角色
	if (CombatInterface)
	{
		const FVector SocketLocation = ICombatInterface::Execute_GetCombatSocketLocation(GetAvatarActorFromActorInfo(),SocketTag);

		FRotator SocketRotation = (ProjectileTargetLocation - SocketLocation).Rotation();//获取目标位置和角色位置的向量，并计算旋转
		
		if (bOverridePitch)		{ SocketRotation.Pitch = PitchOverride;}//是否覆盖角度
		
		Forward = SocketRotation.Vector();//获取面向方向
	}
	return Forward;
	
}

void UAuraFireBolt::SpawnOneProjectile(
	const FVector& ProjectileTargetLocation, const FGameplayTag& SocketTag, AActor* HomingTarget,FRotator Rotator
)
{
	ICombatInterface* CombatInterface = Cast<ICombatInterface>(GetAvatarActorFromActorInfo());//获取角色
	if (CombatInterface)
	{
		const FVector SocketLocation = ICombatInterface::Execute_GetCombatSocketLocation(GetAvatarActorFromActorInfo(),SocketTag);

		{
			//UKismetSystemLibrary::DrawDebugArrow(GetAvatarActorFromActorInfo(), SocketLocation, SocketLocation + Rotator.Vector() * 50.f, 5, FLinearColor::Red, 120, 3);
			FTransform SpawnTransform;
			SpawnTransform.SetLocation(SocketLocation);//设置位置
			SpawnTransform.SetRotation(Rotator.Quaternion());//设置旋转
		
			AAuraProjectile* Projectile = GetWorld()->SpawnActorDeferred<AAuraProjectile>//延迟生成
			(
				ProjectileClass,//指定生成的演员类。
				SpawnTransform,//指定生成位置和旋转。
				GetOwningActorFromActorInfo(),//指定生成的演员的拥有者。
				Cast<APawn>(GetOwningActorFromActorInfo()),//指定生成的演员的归属者。
				ESpawnActorCollisionHandlingMethod::AlwaysSpawn//指定生成的演员的碰撞处理方式。始终生成。
			);
			Projectile->DamageEffectParams = MakeDamageEffectParamsFromClassDefaults();

			//跟踪
			if (HomingTarget && HomingTarget->Implements<UCombatInterface>())
			{
				//设置攻击的位置为攻击对象的根位置
				Projectile->ProjectileMovement->HomingTargetComponent = HomingTarget->GetRootComponent();
			}
			else
			{
				//如果没有获取到攻击目标，则创建一个可销毁的并应用
				Projectile->HomingTargetSceneComponent = NewObject<USceneComponent>(USceneComponent::StaticClass());
				Projectile->HomingTargetSceneComponent->SetWorldLocation(ProjectileTargetLocation); //设置组件位置
				Projectile->ProjectileMovement->HomingTargetComponent = Projectile->HomingTargetSceneComponent;
				
			}
			//设置飞弹朝向目标时的加速度
			Projectile->ProjectileMovement->HomingAccelerationMagnitude = FMath::FRandRange(HomingAccelerationMin, HomingAccelerationMax);
			Projectile->ProjectileMovement->bIsHomingProjectile = bLaunchHomingProjectiles; //设置为true，飞弹将加速飞向攻击目标
			

				
			Projectile->FinishSpawning(SpawnTransform);//完成生成
		}
	}
}


